const Actions = require('./actions');
const DB = require('../../app/db');

import { Storage, monStorage } from 'clientConfig/util/StoreData';
import {updateSession} from 'clientConfig/util/queryurlfield'
import urllist from 'clientConfig/urllist';
import { Control } from 'react-keeper'
const net = require('clientConfig/util/net');
const { Toast } = SaltUI;
const ui = require('clientConfig/util/jsapi/ui')
var save_params= '';
var save_cb='';
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        loaded: false,
    },
    onSubmit: function (params = save_params, cb = save_cb) {
        save_params = params;
        save_cb = cb;
        
        let user = {}
        let modifyData = Storage.get('user.info');
        let userdata = modifyData.user;
        for (let item of params) {
            if (item.modify) {
                user[item.key] = item.content;
                userdata[item.key] = item.content;
            }
        }
         if (user == '{}') return;
        // alert(JSON.stringify(user))
        //  alert(JSON.stringify(modifyData))
        let session = monStorage.get('user.session');
        let url = urllist.setUserInfo + '?session=' + session;
        net.fetchUrl(url, { method: 'post', body: user }).then((jsondata) => {
            // alert(JSON.stringify(jsondata))
            if (jsondata.errcode==0) {      
                 Storage.set('user.info', modifyData);
                 Storage.set('user.modified',userdata.modified);
            } else if (jsondata.errcode == 4097) {
                // alert(4097)
                updateSession(this.onSubmit);
            } else if (jsondata.errcode == 4098) {
                // Toast.show({
                //     type: 'error',
                //     content: '会话id超时'
                // });
                updateSession(this.onSubmit);
            }
        });
    },
    updateComponent: function () {
        this.trigger(this.data);
    },

    getInitialState: function () {
        return this.data;
    }
});
